using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Mail;
using System.Net.Mime;

namespace MailClient
{
    class SmtpService
    {
        
        public String strTo;
        public String strFrom;
        public String strFromname;
        public String strSubject;
        public String strSmtpserver;
        public SmtpClient smtpClient;
        public String strBody;
        private String[] attachments;
        public SmtpService()
        {
            smtpClient = new SmtpClient();
            
        }
        public String SendMail()
        {
            String s = "";
            Boolean b = true;
            MailAddress to = new MailAddress("default@default.com");
            try
            {
                to = new MailAddress(StrTo);
            }
            catch
            {
                s+="Error in recipients e-mail address \n";
                b = false;
            }
            MailAddress from = new MailAddress("default@default.com");
            MailAddress fromname = new MailAddress("default@default.com");
            try
            {
                from = new MailAddress(StrFrom, StrFromname);
                fromname = new MailAddress(StrFrom, StrFromname);
            }
            catch
            {
                s += "Error in senders e-mail address \n";
                b = false;
            }
            MailMessage mailMessage = new MailMessage(from, to);
            Sending sendform = new Sending();
            sendform.Show();
            if (b)
            {
                mailMessage.Sender = fromname;
                mailMessage.Body = StrBody;
                mailMessage.Subject = StrSubject;
                for (int i = 0; i < attachments.Length; i++)
                {

                    try
                    {

                        Attachment temp = new Attachment(attachments[i], MediaTypeNames.Application.Octet);
                        ContentDisposition disposition = temp.ContentDisposition;
                        disposition.CreationDate = System.IO.File.GetCreationTime(attachments[i]);
                        disposition.ModificationDate = System.IO.File.GetLastWriteTime(attachments[i]);
                        disposition.ReadDate = System.IO.File.GetLastAccessTime(attachments[i]);
                        mailMessage.Attachments.Add(temp);
                    }
                    catch
                    { }
                }
                smtpClient.Host = StrSmtpserver;
                smtpClient.Port = 25;
                try
                {
                    smtpClient.Send(mailMessage);
                    s+= "Mail succesfully sent!";
                }
                catch (Exception exc)
                {
                    s += ("Error" + exc.ToString());
                }
            }
            sendform.Hide();
            return s;
        }
        public String SendMail(int number)
        {
            String s = "";
            Boolean b = true;
            MailAddress to = new MailAddress("default@default.com");
            try
            {
                to = new MailAddress(StrTo);
            }
            catch
            {
                s += "Error in recipients e-mail address \n";
                b = false;
            }
            MailAddress from = new MailAddress("default@default.com");
            MailAddress fromname = new MailAddress("default@default.com");
            try
            {
                from = new MailAddress(StrFrom, StrFromname);
                fromname = new MailAddress(StrFrom, StrFromname);
            }
            catch
            {
                s += "Error in senders e-mail address \n";
                b = false;
            }
            MailMessage mailMessage = new MailMessage(from, to);
            Sending sendform = new Sending();
            sendform.Show();
            if (b)
            {
                for (int i = 0; i < number; i++)
                {
                    mailMessage.Sender = fromname;
                    mailMessage.Body = StrBody;
                    mailMessage.Subject = StrSubject;
                    smtpClient.Host = StrSmtpserver;
                    smtpClient.Port = 25;
                    try
                    {
                        smtpClient.Send(mailMessage);
                        s += "Mail succesfully sent! \n";
                    }
                    catch (Exception)
                    {
                        //s += ("Error" + exc.ToString() +"\n");
                        s += "Error in mail: " + (i+1);
                    }
                }
                
            }
            sendform.Hide();
            return s;
        }
        public String StrTo
        {
            get { return strTo; }
            set { strTo = value; }
        }
        public String StrFrom
        {
            get { return strFrom; }
            set { strFrom = value; }
        }
        public String StrFromname
        {
            get { return strFromname; }
            set { strFromname = value; }
        }
        public String StrSubject
        {
            get { return strSubject; }
            set { strSubject = value; }
        }
        public String StrSmtpserver
        {
            get { return strSmtpserver; }
            set { strSmtpserver = value; }
        }
        public String StrBody
        {
            get { return strBody; }
            set { strBody = value; }
        }
        public String[] Attachments
        {
            get { return attachments; }
            set { attachments = value; }
        }
    }
}
